﻿using SuperFrame.Charts.Points;
using System.Windows;
using System.Windows.Media;

namespace SuperFrame.Charts
{
    /// <summary>
    /// Spline Series for Sparrow Charts
    /// </summary>
    public class SeriesSpline : BaseLineSeries
    {
        public SeriesSpline()
        {
            SplinePoints = new PointCollection();
        }


        override public void GenerateDatas()
        {
            isPointsGenerated = false;
            SplinePoints.Clear();
            if (!isPointsGenerated)
                Parts.Clear();
            if (this.Points != null && this.seriesContainer != null)
            {
                CalculateMinAndMax();
                IntializePoints();
                foreach (ChartPoint point in Points)
                {
                    Point linePoint = NormalizePoint(new Point(point.XValue, point.YValue));
                    SplinePoints.Add(linePoint);
                }

                if (this.RenderingMode == RenderingMode.Default)
                {
                    if (!isPointsGenerated)
                    {
                        for (int i = 0; i < this.SplinePoints.Count; i++)
                        {
                            var zhong = new Point(i == 0 ? SplinePoints[i].X / 2 : (SplinePoints[i - 1].X + SplinePoints[i].X) / 2, i == 0 ? SplinePoints[i].Y / 2 : (SplinePoints[i - 1].Y + SplinePoints[i].Y) / 2);

                            var firContr = new Point(zhong.X, i == 0 ? 0 : SplinePoints[i - 1].Y);
                            var endContr = new Point(zhong.X, SplinePoints[i].Y);

                            PartSpline spPartLine = new PartSpline(SplinePoints[i], firContr, endContr, this.Points[i].YValue+"");
                            SetBindingForStrokeandStrokeThickness(spPartLine);
                            this.Parts.Add(spPartLine);
                        }
                        isPointsGenerated = true;
                    }
                    else
                    {
                        int i = 0;
                        foreach (PartSpline part in this.Parts)
                        {
                            var zhong = new Point(i == 0 ? SplinePoints[i].X / 2 : (SplinePoints[i - 1].X + SplinePoints[i].X) / 2, i == 0 ? SplinePoints[i].Y / 2 : (SplinePoints[i - 1].Y + SplinePoints[i].Y) / 2);
                            var firContr = new Point(zhong.X, i == 0 ? 0 : SplinePoints[i - 1].Y);
                            var endContr = new Point(zhong.X, SplinePoints[i].Y);

                            part.point = SplinePoints[i];
                            part.firstControlPoint = firContr;
                            part.endControlPoint = endContr;
                            part.Refresh();
                            i++;
                        }
                    }
                }
                this.seriesContainer.Invalidate();
            }
            isRefreshed = false;
        }

        internal override SeriesContainer CreateContainer()
        {
            return new ContainerSpline();
        }

        public PointCollection SplinePoints
        {
            get { return (PointCollection)GetValue(SplinePointsProperty); }
            set { SetValue(SplinePointsProperty, value); }
        }
        public static readonly DependencyProperty SplinePointsProperty = DependencyProperty.Register("SplinePoints", typeof(PointCollection), typeof(SeriesSpline), new PropertyMetadata(null));


        /// <summary>
        /// 是否显示曲线图上圆点
        /// </summary>
        public bool IsShowPoint
        {
            get { return (bool)GetValue(IsShowPointProperty); }
            set { SetValue(IsShowPointProperty, value); }
        }
        public static readonly DependencyProperty IsShowPointProperty = DependencyProperty.Register("IsShowPoint", typeof(bool), typeof(SeriesSpline), new PropertyMetadata(true));


    }
}
